package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.mapper.SetMealMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetMealService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private JedisPool jedisPool;
    @Autowired
    private SetMealMapper setMealMapper;
    @Autowired
    private CheckGroupMapper checkGroupMapper;
    @Autowired
    private CheckItemMapper checkItemMapper;
    /**
     * 新增套餐
     * @param setmeal
     * @param checkgroupIds
     */
    @Override
    public void addSetMeal(Setmeal setmeal, Integer[] checkgroupIds) {
        //先向t_setmeal表中添加一条数据,需要返回记录生成的主键，提供联合主键使用
        setMealMapper.addSetMeal(setmeal);

        //向套餐和检查项的中间的表中添加数据
        addSetMealAndCheckGroup(checkgroupIds,setmeal.getId());

        //判断图片是否上传到数据库
        if (setmeal.getImg()!=null){
            jedisPool.getResource().sadd("SETMEAL_PIC_DB_RESOURCES",setmeal.getImg());
        }
    }

    private  void addSetMealAndCheckGroup(Integer[] checkgroupIds,Integer setmealId){
        //检查组的id数组不等于0时 循环的根据检查组的id和所选择的检查组的id 向中间表中添加
        if (checkgroupIds!=null && checkgroupIds.length>0){
            for (Integer checkgruopId:checkgroupIds){
                setMealMapper.addSetMealAndCheckGroup(setmealId,checkgruopId);
            }
        }
    }

    //分页查询
    @Override
    public Map findPage(QueryPageBean queryPageBean) {
        Map map=new HashMap();
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        List<Setmeal> setmealList = setMealMapper.findPage(queryPageBean.getQueryString());
        PageInfo<Setmeal> pageInfo=new PageInfo<Setmeal>(setmealList);
        map.put("itemList",pageInfo.getList());
        map.put("total",pageInfo.getTotal());
        return map;
    }

    //根据检查组的id查询出对应检查项的id
    @Override
    public Integer[] findCheckgroupIdsBySetMealId(Integer id) {
        Integer[] checkgroupIds=setMealMapper.findCheckgroupIdsBySetMealId(id);
        return checkgroupIds;
    }

    //获取对应id的套餐数据
    @Override
    public Setmeal findAllSetMeal(Integer id) {
       Setmeal setmeal= setMealMapper.findAllSetMeal(id);
        return setmeal;
    }

    //编辑检查组
    @Override
    public void editsetmeal(Setmeal setmeal, Integer[] checkgroupIds) {
        //先删除之前向t_setmeal_checkgroup添加的id
        setMealMapper.deleteAssociation(setmeal.getId());
        //再向中间表添加新的id
        addSetMealAndCheckGroup(checkgroupIds,setmeal.getId());
        //编辑更新检查组
        setMealMapper.editSetMeal(setmeal);

    }


    //查询手机端所有套餐数据
    @Override
    public List<Setmeal> findAllSetMealMobilWeb() {
        List<Setmeal> setmealList=setMealMapper.findAllSetMealMobilWeb();
        return setmealList;
    }

    //获取手机端套餐详情，包含对应的检查组以及检查组对应的检查项
    @Override
    public Setmeal findSetMailDetail(Integer id) {
        //根据套餐id获取对应的套餐数据
        Setmeal setmeal=setMealMapper.finSetMealById(id);
        //根据套餐id获取对应检查组数据
        List<CheckGroup> checkGroupList= checkGroupMapper.findCheckGroupBySetMealId(setmeal.getId());

        //根据检查组id获取对应检查项的数据
        for (CheckGroup checkGroup : checkGroupList) {
            List<CheckItem> checkItemList=checkItemMapper.findCheckItemByCheckGroupId(checkGroup.getId());
            checkGroup.setCheckItems(checkItemList);
        }
        setmeal.setCheckGroups(checkGroupList);
        return setmeal;
    }
}
